#include <iostream>
using namespace std;

class Gadget {
private:
    int x, y, z, sum;
    bool good;

    void Validate();
    void SumData() { sum = x + y + z; }

public:
    Gadget();
    ~Gadget();
    bool GetData();
    void WriteData();
};

int main() {
    cout << "Gadget程序!\n";

    Gadget red, blue;
    bool goodRed, goodBlue;

    cout << "red Gadget:\n";
    goodRed = red.GetData();

    cout << "blue Gadget:\n";
    goodBlue = blue.GetData();

    if (goodRed && goodBlue) {
        cout << "两个gadget对象中的数据都合法!\n";

        cout << "red Gadget:\n";
        red.WriteData();

        cout << "blue Gadget:\n";
        blue.WriteData();
    }
    else if (goodRed) {
        cout << "red的数据合法,但blue的数据不合法。\n";
        cout << "red Gadget:\n";
        red.WriteData();
    }
    else if (goodBlue) {
        cout << "blue的数据合法,但red的数据不合法。\n";
        cout << "blue Gadget:\n";
        blue.WriteData();
    }
    else {
        cout << "Gadget数据非法。\n";
    }
    cout << "完毕\n";
}

Gadget::Gadget() {
    cout << "Gadget构造函数, 把所有值设置为0\n";
    x = y = z = sum = 0;
    good = false;
}

Gadget::~Gadget() {
    cout << "析构函数\n"
        << "无事可做了，再见\n";
}

bool Gadget::GetData() {
    good = false;

    cout << "请输入Gadget的整数数据成员 x, y, z: ";
    cin >> x >> y >> z;

    Validate();
    if (good) {
        SumData();
    }
    return good;
}

void Gadget::Validate() {
    good = (x > 0 && y > 0 && z > 0);
}

void Gadget::WriteData() {
    cout << "Gadget的数据是: x = " << x
        << ", y = " << y
        << ", z = " << z
        << ", 和为: " << sum << "\n";
}